<script lang="ts">
    export default {
        name: 'couponBox',
    };
</script>

<script setup lang="ts">
    import { computed, onMounted, ref, unref, reactive } from 'vue';
    import { showConfirmDialog, showToast, showLoadingToast, closeToast } from 'vant';
    import { useRouter } from 'vue-router'
    import API_ORDER from '@/apis/order';
    import { useI18n } from 'vue-i18n';
    const { t } = useI18n();
    onMounted(() => { });
    let tabList = ref([
        {
            id: '0',
            name: t('home.CouponCollectionCenter')
        },
        {
            id: '1',
            name: t('home.Received')
        },
        {
            id: '2',
            name: t('home.Used')
        },
        {
            id: '3',
            name: t('home.Expired')
        },
    ])
    let router = useRouter()
    const active = ref(0);
    // 列表相关状态
    const list = ref([]);
    const loading = ref(false);
    const finished = ref(false);
    let params = reactive({
        page: 1,
        limit: 10,
        ids: ''
    })
    let userParams = reactive({
        page: 1,
        limit: 10,
        type: ''
    })
    // 加载更多数据
    const onLoad = async () => {
        loading.value = true;
        if (active.value == 0) {
            let res = await API_ORDER.couponIndex(params)
            if (res.code == 1) {
                list.value = [...list.value, ...res.data.data]
                loading.value = false;
                params.page++
                // 如果数据已全部加载完成
                if (res.data.data.length < params.limit) {
                    finished.value = true;
                }
            }
        } else {
            let res = await API_ORDER.userCouponList(userParams)
            if (res.code == 1) {
                list.value = [...list.value, ...res.data.data]
                loading.value = false;
                params.page++
                // 如果数据已全部加载完成
                if (res.data.data.length < params.limit) {
                    finished.value = true;
                }
            }
        }
    };
    // 切换tab
    let changeTab = () => {
        params.page = 1
        list.value = []
        if (active.value == 1) {
            userParams.type = 'geted'
        } else if (active.value == 2) {
            userParams.type = 'used'
        } else if (active.value == 3) {
            userParams.type = 'invalid'
        }
        onLoad()
    }
    // 领取优惠券
    const receiveCoupon = async (id) => {
        if (active.value == 0) {
            let res = await API_ORDER.couponGet({ id })
            if (res.code == 1) {
                showToast(res.msg);
                params.page = 1
                list.value = []
                onLoad()
            }
        } else {

        }

    };
</script>

<template>
    <div>
        <div class="title">{{$t('home.CouponCollectionCenter')}}</div>
        <div class="couponBox">
            <van-tabs v-model:active="active" @change="changeTab">
                <van-tab :title="i.name" v-for="i in tabList" :key="i.id">
                    <!-- 优惠券列表（触底加载） -->
                    <van-list v-model:loading="loading" :finished="finished" :finished-text="t('home.Nomore')"
                        @load="onLoad">
                        <div v-for="(coupon, index) in list" :key="index" class="couponItem">
                            <div class="flex al-center jus-bet tit">
                                <i class="name">{{active==0?coupon.name:coupon.coupon.name}}</i>
                                <div class="price">$ <span class="num">{{coupon.amount}}</span></div>
                            </div>
                            <div class="flex al-center jus-bet time">
                                <div>{{active==0?coupon.get_start_time:coupon.use_start_time}} ~
                                    {{active==0?coupon.get_end_time:coupon.use_end_time}}</div>
                                <div>{{coupon.amount_text}}</div>
                            </div>
                            <van-divider />
                            <div class="flex al-center jus-bet">
                                <div>
                                    {{active==0?coupon.description:coupon.coupon.description}}
                                </div>
                                <van-button round color="linear-gradient(to right, #ff6305, #ffa670)" size="small"
                                    :disabled="coupon.get_status==='cannot_get'"
                                    @click="receiveCoupon(coupon.id)">{{active==0?coupon.get_status_text:coupon.status_text}}</van-button>
                            </div>
                            <div class="leftIcon">{{coupon.type_text}}</div>
                        </div>
                    </van-list>
                </van-tab>

            </van-tabs>
        </div>
    </div>
</template>

<style lang="less" scoped>
    .title {
        margin: 6px;
        font-size: 18px;
        font-weight: 600;
    }

    .order_list {}

    .couponBox {
        padding: 0 10px;

        .couponItem {
            position: relative;
            margin: 10px 0;
            box-shadow: 1px 1px 5px 1px #ccc;
            border-radius: 10px;
            padding: 16px;

            .leftIcon {
                position: absolute;
                top: 0;
                left: 0;
                background: linear-gradient(to right, #ff6305, #ffa670);
                color: #fff;
                padding: 5px;
                border-top-left-radius: 10px;
                border-bottom-right-radius: 10px;
                font-size: 12px;
            }

            .tit {
                padding: 16px 0 8px 0;

                .name {
                    font-weight: 700;
                    font-size: 20px;
                }

                .price {
                    color: red;
                    font-weight: 600;

                    .num {
                        color: red;
                        font-size: 36px;
                        font-weight: 500;
                    }
                }
            }

            .time {
                font-size: 12px;
                color: #a4a4a4;
            }
        }
    }
</style>